package com.example.rabbitmqspringboot.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * @author zfj
 * @date 2022/4/26 -15:38
 */
@Configuration
public class DelayedQueueConfig {
	//延迟队列
	public static final String DELAYED_QUEUE_NAME = "delayed.queue";
	//交换机
	public static final String DELAYED_EXCHANGE_NAME = "delayed.exchange";
	//routingkey
	public static final String DELAYED_ROUTING_KEY = "delayed.routingkey";

	//队列
	@Bean
	public Queue delayedQueue(){
		return new Queue(DELAYED_QUEUE_NAME);
	}

	//自定义交换机 我们在这里定义的是一个延迟交换机
	@Bean
	public CustomExchange delayedExchange(){
		Map<String, Object> args = new HashMap<>();
		//自定义交换机的类型
		args.put("x-delayed-type", "direct");
		/**
		 * 1.交换机名称
		 * 2.交换机类型
		 * 3.是否需要持久化
		 * 4.是否需要自动删除
		 */
		return new CustomExchange(DELAYED_EXCHANGE_NAME,"x-delayed-message",
				true,false,args);
	}

	@Bean
	public Binding bindingDelayedQueue(@Qualifier("delayedQueue") Queue delayedQueue,
	                                   @Qualifier("delayedExchange") CustomExchange delayedExchange){
		return BindingBuilder.bind(delayedQueue).to(delayedExchange).with(DELAYED_ROUTING_KEY).noargs();


	}

}
